package mes.kanban.respository.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import mes.kanban.respository.RedisRespositoryI;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * redis 服务提供
 * @author gjy
 *
 * 2018年1月16日下午5:03:41
 */
@Service
public class RedisRespositoryImpl implements RedisRespositoryI{
	
	
	private static final Logger log = LoggerFactory.getLogger(RedisRespositoryImpl.class);

	@Autowired
	private JedisPool jedisPool;
	
	@Override
	public String getValue(String key) {
		
		Jedis jedis = null;
		String value = null;
		try {
			jedis = jedisPool.getResource();
			value = jedis.get(key);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("redis 异常:"+e.getMessage());
		}finally {
			jedis.close();
		}
		return value;
	}

	@Override
	public void setValue(String key, String value) {
		
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.set(key, value);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("redis 异常:"+e.getMessage());
		}finally {
			jedis.close();
		}
	}

	@Override
	public String hget(String hkey, String key) {
		Jedis jedis = jedisPool.getResource();
		String value = jedis.hget(hkey, key);
		jedis.close();
		return value;

	}

	@Override
	public void hset(String hkey, String key, String value) {
		Jedis jedis = jedisPool.getResource();
		log.info("*********redisping:"+jedis.ping());
		jedis.hset(hkey, key, value);
		jedis.close();

	}

	@Override
	public long incr(String key) {
		Jedis jedis = jedisPool.getResource();
		log.info("*********redisping:"+jedis.ping());
		Long result = jedis.incr(key);
		jedis.close();
		return result;

	}

	@Override
	public long expire(String key, int second) {
		Jedis jedis = jedisPool.getResource();
		log.info("*********redisping:"+jedis.ping());
		Long result = jedis.expire(key, second);
		jedis.close();
		return result;

	}

	@Override
	public long ttl(String key) {
		Jedis jedis = jedisPool.getResource();
		log.info("*********redisping:"+jedis.ping());
		Long result = jedis.ttl(key);
		jedis.close();
		return result;

	}

	@Override
	public Jedis getjedis() {
		return jedisPool.getResource();
	}
	
	
	
}
